// 数据处理
var formidable = require('formidable');
const { searchFriends } = require('../sql/searchFriends')
const { login, VerifyMailbox, Register } = require('../sql/login')
const { addfriends, searchUser, userInof, editUser, addGroup, groupQuery, queryFriendsList, userId, yesfriends } = require('../sql/addfriends')


module.exports = (app) => {
    // 登录
    app.post('/login', async(req, res) => login(req, res))

    // 验证邮箱
    app.post('/yanzheng', async(req, res) => VerifyMailbox(req, res))

    // 注册
    app.post('/register', async(req, res) => Register(req, res))

    // 搜索好友
    app.post('/searchfriends', (req, res) => searchFriends(req, res))

    // email || name 搜索
    app.post('/searchuser', (req, res) => searchUser(req, res))

    // 搜索好友id查询用户信息
    app.post('/searchuser/id', (req, res) => userInof(req, res))
        // 搜索好友id查询用户信息
    app.get('/userid', (req, res) => userId(req, res))

    // 添加好友
    app.post('/addfriends', (req, res) => addfriends(req, res))

    //同意好友
    app.post('/yesfirend', (req, res) => yesfriends(req, res))

    // 分组创建
    app.post('/addgroup', (req, res) => addGroup(req, res))

    // 分组查询
    app.get('/groupquery', (req, res) => groupQuery(req, res))

    // 查询好友列表
    app.get('/queryfriendslist', (req, res) => queryFriendsList(req, res))
        // 修改资料
    app.post('/editUser', (req, res) => editUser(req, res))
    app.post('/formData', (req, res) => {
        let time = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + '-' + new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds()

        let { f_id, u_id, val, inof, uname, type, img, mediaRecorder, location, id, formData, duration } = req.body
        console.log(req.body);

        const chat = { type, title: 'ws', name: inof.user_name || uname, msg: '语音', formData, time, duration, f_id: u_id, img: inof.user_img, id: u_id }

        const form = new formidable.IncomingForm();
        //解析客户端传递过来的formData对象
        form.parse(formData, (err, fields, files) => {
            //req:请求对象，err错误对象，filelds：普通请求参数的内容
            //files：文件的内容
            console.log(fields);
        })
    })

    // 发送图片

}